unit MyTimer;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls, ExtCtrls,DBModule,ErrWork,DateUtils;

type
  TForm7 = class(TForm)
    Timer1: TTimer;
    Label1: TLabel;
    DateTimePicker1: TDateTimePicker;
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form7: TForm7;
  MyOwnTime:TDateTime;
  SQS_SyS_Diff:TDateTime;
  MySecond:TDateTime;
  procedure StartValue();
  function ChkTime:integer;
  function DateClt(First_Time:TDateTime;Second_Time:TDateTime;Dif_Time:TDateTime):integer;
implementation

{$R *.dfm}



procedure StartValue();
var SQLTime,SySTime:TDateTime;
    DifYear,SQLYear,SySYear:Word;
    DifMonth,SQLMonth,SySMonth:Word;
    DifDay,SQLDay,SySDay:Word;
    DifHour,SQLHour,SySHour:Word;
    DifMinute,SQLMinute,SysMinute:Word;
    DifSecond,SQLSecond,SYSSecond:Word;
    Trash:Word;
const noll=0;
      odin=1;
begin
  If SelectDateTime(SQLTime)=1
    then
      begin
        SySTime:=now;
//        EncodeDateTime
        DecodeDateTime(SQLTime,SQLYear,SQLMonth,SQLDay,SQLHour,SQLMinute,SQLSecond,Trash);
        DecodeDateTime(SySTime,SySYear,SySMonth,SySDay,SySHour,SySMinute,SySSecond,Trash);
        DifYear:=SQLYear-SySYear;
        DifMonth:=SQLMonth-SySMonth;
        DifDay:=SQLDay-SySDay;
        DifHour:=SQLHour-SySHour;
        DifMinute:=SQLMinute-SysMinute;
        DifSecond:=SQLSecond-SYSSecond;
        SQS_SyS_Diff:=EncodeDateTime(DifYear,DifMonth,DifDay,DifHour,DifMinute,DifSecond,noll);
        MySecond:=EncodeDateTime(noll,noll,noll,noll,noll,odin,noll);
        Form7.Timer1.Enabled:=True;
        MyOwnTime:=Now;
      end
    else ErrExcept(mg_GetSQLTimeErr);
end;

procedure TForm7.Timer1Timer(Sender: TObject);
begin
  MyOwnTime:=MyOwnTime+MySecond;
  With Form7 do
    begin
      DateTimePicker1.DateTime:=MyOwnTime;
      Label1.Caption:=TimeToStr(MyOwnTime);
    end
end;

function DateClt(First_Time:TDateTime;Second_Time:TDateTime;Dif_Time:TDateTime):integer;
var Dif_Year,First_Year,Second_Year:Word;
    Dif_Month,First_Month,Second_Month:Word;
    Dif_Day,First_Day,Second_Day:Word;
    Dif_Hour,First_Hour,Second_Hour:Word;
    Dif_Minute,First_Minute,Second_Minute:Word;
    Dif_Second,First_Second,Second_Second:Word;
    Trash:Word;
begin
  DecodeDateTime(First_Time,First_Year,First_Month,First_Day,First_Hour,First_Minute,First_Second,Trash);
  DecodeDateTime(Second_Time,Second_Year,Second_Month,Second_Day,Second_Hour,Second_Minute,Second_Second,Trash);
  DecodeDateTime(Dif_Time,Dif_Year,Dif_Month,Dif_Day,Dif_Hour,Dif_Minute,Dif_Second,Trash);
  First_Second:=trunc(First_Second/10)*10;
  Second_Second:=trunc(Second_Second/10)*10;
  Dif_Second:=trunc(Dif_Second/10)*10;
  IF Second_Year+Dif_Year=First_Year
    then
      IF Second_Month+Dif_Month=First_Month
        then
          IF Second_Day+Dif_Day=First_Day
            then
              IF Second_Hour+Dif_Hour=First_Hour
                then
                  IF Second_Minute+Dif_Minute=First_Minute
                    then
                      If Second_Second+Dif_Second=First_Second
                        then result:=1
                        else result:=-1
                    else result:=-1
                else result:=-1
            else result:=-1
        else result:=-1
    else result:=-1;
end;

function ChkTime:integer;
var SQLTime,SySTime:TDateTime;
const noll=0;
begin
  If SelectDateTime(SQLTime)=1
    then
      begin
        SySTime:=Now;
        If DateClt(SQLTime,SySTime,SQS_SyS_Diff)=-1
          then ErrExcept(mg_NoSinhroErr);
        IF DateClt(SySTime,MyOwnTime,noll)=-1
          then ErrExcept(mg_ChangeTimeErr);
      end
    else ErrExcept(mg_GetSQLTimeErr);
end;
end.
